const db = require("../db/index");
const moment = require('moment')

exports.dictionarylists = (req, res) => {
    var sql =
        "select * from dictionary";
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};

exports.dictionarydetaillists = (req, res) => {
    var sql =
        `select * from dictionarydetail where dictionary_id=${req.query.id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};

exports.dictionarydetailinsert = (req, res) => {
    const { id , name , note , status} =req.body
    var sql =
        `insert into dictionarydetail(dictionary_id,name,status,updatetime,note) value(${id},'${name}','${status}','${moment(new Date()).format("YYYY-MM-DD HH:mm:ss")}','${note?note:''}')`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `select * from dictionarydetail where dictionary_id=${id}`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }
                res.send(data);
            });
        }
    });
};

exports.dictionarydetailupdate = (req, res) => {
    const { id , name , note , status , dictionarydetail_id} =req.body
    console.log(req.body)
    var sql =
        `update dictionarydetail set name='${name}',status='${status}',updatetime='${moment(new Date()).format("YYYY-MM-DD HH:mm:ss")}',note='${note?note:''}' where dictionarydetail_id=${dictionarydetail_id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `select * from dictionarydetail where dictionary_id=${id}`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }
                res.send(data);
            });
        }
    });
};


exports.dictionarydetaildel = (req, res) => {
    const { id , dictionarydetail_id } = req.body
    var sql =
        `delete from dictionarydetail where dictionarydetail_id=${dictionarydetail_id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `select * from dictionarydetail where dictionary_id=${id}`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }
                res.send(data);
            });
        }
    });
};
